package media
{
	import events.MyEvent;
	
	import flash.display.Stage;
	import flash.events.*;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.events.StageVideoAvailabilityEvent;
	import flash.events.StageVideoEvent;
	import flash.geom.Rectangle;
	import flash.media.SoundTransform;
	import flash.media.StageVideo;
	import flash.media.StageVideoAvailability;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import utils.Analyzer;
	/**
	 * ...
	 * @author FireKnight
	 */
	public class VideoManager extends EventDispatcher
	{
		private var ns:NetStream;
		private var nc:NetConnection;
		private var video:Video;
		private var movie_url:String;
		private var duration:Number;
		private var soundTrans:SoundTransform=new SoundTransform();
		public function VideoManager( target:IEventDispatcher=null)
		{
			super(target);
				//showMovie(s);
			trace("vm版本1.0 2015.3.23");
			trace("vm版本1.1 2015.3.25");
			trace("vm版本1.2 2015.3.28");
			trace("vm版本1.3 2015.8.31");
		}
		
		
		
		public function showStageVideo(s:String,_stage:Stage,w:Number,h:Number,yl:Number=1):void
		{
			movie_url=s;
			nc = new NetConnection();
			nc.connect(null);
			ns = new NetStream(nc);
			soundTrans.volume=yl;
			ns.soundTransform=soundTrans;
			ns.client = this;
			trace("use stageVideo");
			trace("指定显示的数据是否进行缓存以供智能搜索:"+ns.inBufferSeek);
			ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler,false,0,true);
			//ns.play(s);
		}
		
		private function  onRenderState(e:StageVideoEvent):void 
		{
			trace("fps="+ns.currentFPS);
		}
		
		public function showRemoteMovie(rtmpUrl:String,yl:Number=1):void
		{
			
		}
		
		public function showMovie(s:String,yl:Number=1):void
		{
			trace("showMovie,"+s,yl);
			movie_url=s;
			try
			{
				nc = new NetConnection();
				nc.connect(null);
			}
			catch(e:Error)
			{
				trace(e.message);
			}
			try
			{
				ns = new NetStream(nc);
			}
			catch(e:Error)
			{
				trace(e.message);
			}
			soundTrans.volume=yl;
			ns.soundTransform=soundTrans;
			ns.client = this;
			ns.bufferTime=3;
			ns.bufferTimeMax=30;
			ns.maxPauseBufferTime=120;
			ns.step(1);
			ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler,false,0,true);
			trace(ns,nc);
			video = new Video();
			video.smoothing=true;
			video.attachNetStream(ns);
			//ns.play(s);
		}
		
		
		
		public function onPlayStatus( info:Object ):void{ 
			switch ( info.code ){ 
				case "NetStream.Play.Switch": 
					break; 
				case "NetStream.Play.Complete": 
					trace("Complete"); 
					break; 
				default: 
					for ( var a:String in info ) {  
						trace(a + " " + info[a]); 
					} 
					break; 
			} 
		} 
		
		private function clearVideo():void
		{
			if(video)
			{
				video = null;
			}
			cleartStream();
		}
		
		private function cleartStream():void
		{
			ns.removeEventListener(NetStatusEvent.NET_STATUS, statusHandler);
			ns.dispose();
			nc.close();
			ns = null;
			nc = null;
		}
		

		
		public function onMetaData(infoObject:Object):void
		{
//			trace("////onMetaData////");
//			Analyzer.start(infoObject);
//			trace("////onMetaData end////");
			trace("影片有"+infoObject.duration+"秒");
			trace("metadata: duration=" + infoObject.duration + " width=" + infoObject.width + " height=" + infoObject.height + " framerate=" + infoObject.framerate);
			duration=infoObject.duration;
			this.dispatchEvent(new MyEvent("movieSizeGeted",{width:infoObject.width,height:infoObject.height,duration:infoObject.duration}));
		}
		
		public function onXMPData(infoObject:Object):void
		{
			//trace("////onXMPData////");
			////Analyzer.start(infoObject);
			//trace("////onXMPData end////");
		}
		
		private function statusHandler(e:NetStatusEvent):void
		{
			
			if (e.info.code == "NetStream.Play.Complete")
			{
				trace("播放完了,"+e.info.code);
				this.dispatchEvent(new MyEvent("playEnd"));
			}
			else if(e.info.code=="NetStream.Play.Stop" && uint(duration)==uint(ns.time)&&(duration>0&&ns.time>0))
			{
				trace("播放完了,"+e.info.code);
				this.dispatchEvent(new MyEvent("playEnd"));
			}
			else if(e.info.code=="NetStream.Buffer.Empty")
			{
				trace("缓存不足,"+e.info.code);
				this.dispatchEvent(new MyEvent("BufferEmpty"));
			}
			else if(e.info.code=="NetStream.Buffer.Full")
			{
				trace("缓存充足,"+e.info.code);
				this.dispatchEvent(new MyEvent("BufferFull"));
			}

		}
		
		public function togglePause():void
		{
			if(video)
			{
				ns.togglePause();
			}
		}
		
		public function stop():void
		{
			ns.close();	
			trace("close");
		}
		
		
		public function pause():void
		{
			ns.pause();
			trace("pause");
		}
		
		public function resume():void
		{
			ns.resume();
			trace("resume");
		}
		
		public function play():void
		{
			if(video)
			{
				trace("video");
				ns.play(movie_url);
			}
			trace("movie_url="+movie_url);
		}
		
		public function setVolume(s:Number):void
		{
			soundTrans.volume=s;
			ns.soundTransform=soundTrans;
		}
		
		public function gotoAndPlay(s:uint):void
		{
			ns.seek(s);
		}
		
		public function get _video():*
		{
			return video;
		}
		
		public function onTimeCoordInfo(obj:*=null):void
		{
			
		}
		
		private function onAsyncErrorHandler(evt:AsyncErrorEvent):void{};
		
		public function get totalTime():Number
		{
			return duration;
		}
		
		public function gc():void
		{
			clearVideo();
		}
		
		public function get time():Number
		{
			if(ns)
			{
				return ns.time;
			}
			else
			{
				return 0;
			}
		}
	
	}

}